package com._21cn.cms.site.web.controller;

import com._21cn.cms.common.FormSelector;
import com._21cn.cms.site.entity.Site;
import com._21cn.cms.site.service.SiteService;
import com._21cn.framework.base.controller.BaseController;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/admin/site/site")
public class SiteController extends BaseController{
    private static String listView = "/admin/site/site/list";
    private static String showView = "/admin/site/site/show";
    private static String jsonView = "/admin/site/site/json";
    private static String formView = "/admin/site/site/form";
    private static String redirectListView = "redirect:/admin/site/site/list.do";

    private Map<String, String> propertyTitleMap = null;

    @Autowired(required = false)
    private SiteService siteService;

    @Autowired(required = false)
    @Qualifier("siteValidator")
    private Validator siteValidator;

    @RequestMapping("/list")
    public String listSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", listView);
        setRequestModelMap( request, model, true );
        model.addAttribute( "statusMap", FormSelector.getStatusMap() );
        return view;
    }

    @RequestMapping("/search")
    public String searchSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", jsonView );
        setRequestModelMap( request, model, true );
        PageList<Site> result = getPageList( model );
        model.addAttribute( "objListJson" , com.alibaba.fastjson.JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat) );
        return view;
    }

    @RequestMapping("/show")
    public String showSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", showView );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            Site site = siteService.getById( id );
            if( site == null ){
                model.put( "error.param.wrong", "无效参数" );
            }
            model.addAttribute( "statusMap", FormSelector.getStatusMap());
            model.addAttribute( "site", site );
        }
        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping("/edit")
    public String editSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", formView );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        Site site = new Site();
        if( id > 0 ){
            site = siteService.getById(id);
            if( site == null ){
                model.put( "error.param.wrong", "无效参数" );
            }
        }
        model.addAttribute( "site", site );
        model.addAttribute( "statusMap", FormSelector.getStatusMap() );
        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping("/save")
    public String saveSite(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id>0 ){                                         //更新
            Site site = siteService.getById( id );
            if( bind( request, site, model, siteValidator).hasErrors() ){
                return formView;
            }
            siteService.update( site );
        }else{                                              //添加
            Site site = new Site();
            if( bind( request, site, model, siteValidator).hasErrors() ){
                return formView;
            }
            siteService.insert( site );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }

    @RequestMapping("/updateStatus")
    public String updateSiteStatus(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);

        Integer status = reqInfo.getIntParameter( "status", -1 );
        if( status < 0 ){
            model.clear();
            return redirectListView+ "?" + reqInfo.getParameter( "queryParam", "" );
        }

        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds( reqInfo, model );
        if( objectIds.length() > 0 ){
            siteService.update( model );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }

    @RequestMapping("/delete")
    public String deleteSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);

        String objectIds = getGetObjectIds( reqInfo, model );
        if( objectIds.length() > 0 ){
            siteService.delete( model );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }

    @RequestMapping("/select")
    public String selectSite(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", jsonView );
        setRequestModelMap( request, model, true );
        PageList<Site> result = getPageList( model );
        model.addAttribute( "objListJson" , com.alibaba.fastjson.JSON.toJSONString(result) );
        return view;
    }

    private PageList<Site> getPageList(ModelMap model) {
        if(model.containsKey("likeName"))
            model.put("likeName",model.get("likeName").toString().toLowerCase());
        PageList<Site> siteList = siteService.getList( model, Integer.parseInt(model.get("pageNo").toString()), Integer.parseInt(model.get("pageSize").toString()) );
        model.addAttribute( "siteList", siteList);
        model.addAttribute( "objList", siteList);
        model.addAttribute( "pageTurn", siteList.getPageTurn());
        model.addAttribute( "titleMap", getPropertyTitleMap() );
        return siteList;
    }

    private Map<String, String> getPropertyTitleMap() {
        if( propertyTitleMap == null ){
            propertyTitleMap = new HashMap<String,String>();
            propertyTitleMap.put( "id", "站点ID" );
            propertyTitleMap.put( "orgId", "机构ID" );
            propertyTitleMap.put( "name", "站点名称" );
            propertyTitleMap.put( "code", "站点代号" );
            propertyTitleMap.put( "domainName", "站点域名" );
            propertyTitleMap.put( "path", "保存路径" );
            propertyTitleMap.put( "logoUrl", "Logo" );
            propertyTitleMap.put( "status", "状态" );
            propertyTitleMap.put( "memo", "备注" );
            propertyTitleMap.put( "modifyUserId", "修改人" );
            propertyTitleMap.put( "modifyTime", "修改时间" );
            propertyTitleMap.put( "createTime", "创建时间" );
            propertyTitleMap.put( "createUserId", "创建人" );
        }
        return propertyTitleMap;
    }


}
